/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.enac.geoapp.gui.custom.component;

import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JComponent;
import javax.swing.JWindow;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

/**
 * 
 * @author mathieu
 * Part of custom component for selecting an item in a list
 */
@SuppressWarnings("serial")
public class RangeSelectorComponent extends JComponent implements ActionListener,
		AncestorListener {
	private JComponent dropDownComp;
	private ImageLabelButton selectedRange;
	private ImageButton arrow;
	private JWindow popup;

	public RangeSelectorComponent(JComponent ddcomp, int initRange) {
		this.dropDownComp = ddcomp;
		this.selectedRange = new ImageLabelButton(
				"images/black_range.png", "Range: " + initRange + "NM");
		this.selectedRange.setFont(new Font("SansSerif", Font.BOLD, 20));
		this.arrow = new ImageButton("images/down_arrow.png");

		arrow.addActionListener(this);
		addAncestorListener(this);

		setupLayout();
	}

	private void setupLayout() {
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		setLayout(gbl);

		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.BOTH;
		gbl.setConstraints(selectedRange, gbc);
		add(selectedRange);
		
		gbc.gridy = 1;
		gbl.setConstraints(arrow, gbc);
		add(arrow);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (popup != null && popup.isVisible()) {
			popup.setVisible(false);

		} else {
			popup = new JWindow(getFrame(null));
			
			popup.getContentPane().add(dropDownComp);
			popup.addWindowFocusListener(new WindowAdapter() {
				@Override
				public void windowLostFocus(WindowEvent e) {
					popup.setVisible(false);
				}

			});
			popup.pack();
			Point pt = selectedRange.getLocationOnScreen();
			pt.translate(0, selectedRange.getHeight());
			popup.setLocation(pt);
			popup.toFront();
			popup.setVisible(true);
			popup.requestFocusInWindow();

		}
	}

	private Frame getFrame(Component comp) {
		if (comp == null) {
			comp = this;
		}
		if (comp.getParent() instanceof Frame) {
			return (Frame) comp.getParent();
		}
		return getFrame(comp.getParent());
	}

	@Override
	public void ancestorAdded(AncestorEvent arg0) {
		hidePopup();
	}

	@Override
	public void ancestorMoved(AncestorEvent arg0) {
		hidePopup();
	}

	@Override
	public void ancestorRemoved(AncestorEvent event) {
		if (event.getSource() != popup)
			hidePopup();
	}

	public void hidePopup() {
		if (popup != null && popup.isVisible()) {
			popup.setVisible(false);
		}
	}

	public void changeValue(String newValue) {
		if (newValue.isEmpty()) {
			hidePopup();
		} else {
			this.selectedRange.setText("Range: " + newValue + "NM");
		}
	}
}
